package com.jamie.jvm.volatile_;

public class T1 {

    volatile int n = 0;

    public void add() {
        n++;
    }



    public static int number = 0;

    /**
     * 开启20个线程，并每个线程执行1000个number++，期望结果为20000
     */
    public static void main(String[] args) {
        for(int i = 0; i < 20; i++){
            new Thread(() -> {
                for(int y = 0; y < 1000; y++){
                    number++;
                }
            }, String.valueOf(i)).start();
        }
        //  需要等待上面20个线程都全部完成后，再用main线程取得最终值，原子性合理的情况是20000
        while(Thread.activeCount() > 2){
            Thread.yield();
        }
        System.out.println(Thread.currentThread().getName() + " finally number value:" + number);
    }

}
